package demo2;

public class StringTest {
    public static void main(String[] args) {
        //test1();
        //test2();
        //test3();
        //test4();
        //test5();
        //test6();
        //test7();
        //test8();
        //test9();
        test10();
    }
    public static void test1() {
        String str1 = "abc";
        String str2 = "abc";
        System.out.println(str1 == str2);
        System.out.println(str1.length());
    }
    public static void test2() {
        String str1 = new String("abc");
        String str2 = new String("abc");
        System.out.println(str1 == str2);
    }
    public static void test3() {
        String s2 = "abc";
        char[] ch = new char[]{'a', 'b', 'c'};
        String s1 = new String(ch);
        s1.intern();
        System.out.println(s1 == s2);
    }
    public static void test4() {
        String name = "zhang";
        name = "zhang" + "san";
        System.out.println(name);
    }
    public static void test5() {
        long start = System.currentTimeMillis();
        String s = "";
        for(int i = 0; i < 10000; ++i){
            s += i;
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);

        start = System.currentTimeMillis();
        StringBuffer sbf = new StringBuffer("");
        for(int i = 0; i < 10000; ++i){
            sbf.append(i);
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);

        start = System.currentTimeMillis();
        StringBuilder sbd = new StringBuilder();
        for(int i = 0; i < 10000; ++i){
            sbd.append(i);
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    public static void test6() {
        String str1 = "+";
        for (int i = 0; i < 100; i++) {
            str1 +="+";//new了100次新对象
        }
        System.out.println(str1);
        //StringBuffer str2 = "+";//不支持这样直接赋值;
        StringBuffer str2 = new StringBuffer("+");
        for (int i = 0; i < 100; i++) {
            str2.append("+");
        }
        System.out.println(str2);
        //StringBuilder str3 = "+";//不支持这样直接赋值;
        StringBuilder str3 = new StringBuilder("+");
        for (int i = 0; i < 100; i++) {
            str3.append("+");
        }
        System.out.println(str3);
    }
    public static void test7() {
        StringBuffer str1 = new StringBuffer("hello");
        str1.append(" word!");
        System.out.println(str1);
        str1.append(2025);
        System.out.println(str1);
    }
    public static void test8() {
        StringBuffer str1 = new StringBuffer("hello");
        System.out.println(str1.deleteCharAt(1));
    }
    public static void test9() {
        StringBuffer str1 = new StringBuffer("hello");
        System.out.println(str1.delete(0,1));
    }
    public static void test10() {
        StringBuffer str1 = new StringBuffer("hello");
        System.out.println(str1.reverse());
    }
}
